package mygame;

//import com.bulletphysics.collision.broadphase.Dbvt.Node;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;

public class Main extends SimpleApplication 
    implements AnimEventListener {

    private AnimChannel channel;
    private AnimControl control;
    private Nifty nifty;
    private static Personagem p1, p2;
    Node player;
  
    public static void main(String[] args) {
        
        Main app = new Main();/*
        AppSettings settings = new AppSettings(true);
        settings.setRenderer(AppSettings.LWJGL_OPENGL1);
        app.setSettings(settings);
        */
        SaveGame svg = SaveGame.abrirSaveGame();
        
        Profile prf = svg.getProfile("Diogo");
        if(prf == null){
            prf = new Profile("Diogo");
        }else{
            prf.getPersonagem().recuperarSangue(20);
        }
        
        System.out.println(prf.getNome() + " esta no nivel " + prf.getNivel() + ".");
        
        //Personagem p1, p2;
//        p1 = new Personagem("p1");
//        p1.addAgilidade(4);
//        p1.addForca(5);
//        p1.addVigor(3);
        p1 = prf.getPersonagem();
        p2 = new Personagem("p2");
        p2.addAgilidade(3);
        p2.addForca(5);
        p2.addVigor(4);
        
        Combate.combate(p1, p2);
        
        prf.setNivel(prf.getNivel()+1);
        svg.salvarProfile(prf);
        System.out.println(prf.getNome() + " aumentou para o nivel " + prf.getNivel() + ".");
        svg.salvar();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        
        /*viewPort.setBackgroundColor(ColorRGBA.LightGray);
        initKeys();
        DirectionalLight dl = new DirectionalLight();
        dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
        rootNode.addLight(dl);
        //player = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
        player = (Node) assetManager.loadModel("Models/p1/p1.j3o");
        player.setLocalScale(0.5f);
        player.rotate(0, 1.5f, 0);
        rootNode.attachChild(player);
        /*
        control = player.getControl(AnimControl.class);
        control.addListener(this);
        channel = control.createChannel();
        channel.setAnim("descansar");    */
        
        loadNifty();
        loadMenuPrincipal();
    }

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
    
    private void loadNifty()
    {
        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
                                                          inputManager,
                                                          audioRenderer,
                                                          guiViewPort);
        nifty = niftyDisplay.getNifty();
        //nifty.fromXml("assets/Interface/PlayerStatus.xml", "start", new PlayerStatus(this, nifty));

        guiViewPort.addProcessor(niftyDisplay);

        //inputManager.setCursorVisible(true);
    }
    
    public void loadMenuPrincipal()
    {
        nifty.fromXml("assets/Interface/MenuPrincipal.xml", "start", new MenuPrincipal(this, nifty));

        inputManager.setCursorVisible(true);
    }
    
    public void loadPersonagemStatus()
    {
        nifty.fromXml("assets/Interface/PersonagemStatus.xml", "start", new PersonagemStatus(this, nifty, p1));

        inputManager.setCursorVisible(true);
    }
    
    public void loadShop()
    {
        //nifty.fromXml("assets/Interface/Shop.xml", "start", new PersonagemStatus(this, nifty));

        inputManager.setCursorVisible(true);
    }
    
  //Quando a animação termina
  public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
    if (animName.equals("correr")) {
      channel.setAnim("descansar", 0.50f);
      channel.setLoopMode(LoopMode.DontLoop);
      channel.setSpeed(1f);
    }
  }
  
  //Durante a animação
  public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
    // unused
  }

  private void initKeys() {
    inputManager.addMapping("correr", new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(actionListener, "correr");
  }
  private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("correr") && !keyPressed) {
        if (!channel.getAnimationName().equals("correr")) {
          channel.setAnim("correr", 0.50f);
          channel.setLoopMode(LoopMode.Loop);
        }
      }
    }
  };
}
